package com.caishi.lkx.exam.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.exam.model.PackageSkuAttrModel;
import com.caishi.lkx.exam.service.ICategoryService;
import com.caishi.lkx.exam.service.IExamService;
import com.caishi.lkx.exam.service.IPackageSkuAttrService;
import com.zzw.common.Wrappers;
import com.zzw.common.exception.BizRuntimeException;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;


@Tag(name = "套餐sku管理")
@Slf4j
@RestController
@RequestMapping("/exam/package/sku/attr")
public class PackageSkuAttrApi extends AdminBaseApi<IPackageSkuAttrService, PackageSkuAttrModel, String> {
    @Resource
    private IPackageSkuAttrService packageSkuAttrService;
    @Resource
    private IExamService examService;
    @Resource
    private ICategoryService categoryService;

    @Override
    public IPackageSkuAttrService loadService() {
        return packageSkuAttrService;
    }

    @PutMapping("/changeStatus")
    public PackageSkuAttrModel changeStatus(@RequestParam String id, @RequestParam Boolean enable) {
        PackageSkuAttrModel model = new PackageSkuAttrModel();
        List<PackageSkuAttrModel> skuAttrModelList = packageSkuAttrService.selectList(Wrappers.<PackageSkuAttrModel>lambdaQuery()
                .eq(PackageSkuAttrModel::getParentId, id).select(PackageSkuAttrModel::getId));
        skuAttrModelList.stream().forEach(e -> {
            PackageSkuAttrModel temp = new PackageSkuAttrModel();
            temp.setId(e.getId());
            temp.setEnable(enable);
            super.update(temp);
        });
        model.setId(id);
        model.setEnable(enable);
        return super.update(model);
    }

    @GetMapping("/getFirstPage")
    public IPage<PackageSkuAttrModel> getFirstPage(Page<PackageSkuAttrModel> page, PackageSkuAttrModel model, @NotNull JSONObject otherParam) {
        IPage<PackageSkuAttrModel> data = loadService().page(loadService().pageProcessing(page), baseWrapper(model, otherParam).isNull(PackageSkuAttrModel::getParentId));
        data.getRecords().parallelStream().peek(r -> {
            if (StrUtil.isBlank(r.getExamId())) {
                r.setExamName("通用");
            } else {
                r.setExamName(examService.queryName(r.getExamId()));
            }
            r.setChildren(loadService().selectList(Wrappers.<PackageSkuAttrModel>lambdaQuery()
                    .eq(PackageSkuAttrModel::getParentId, r.getId())
                    .orderByAsc(PackageSkuAttrModel::getSort)).parallelStream().peek(c -> c.setExamName("--")).collect(Collectors.toList()));
        }).collect(Collectors.toList());
        return data;
    }

    @GetMapping("/allEnableListTree")
    public List<PackageSkuAttrModel> allEnableList(@RequestParam(required = false) String examId, @RequestParam(required = false) Long categoryId) {
        List<PackageSkuAttrModel> packageSkuAttrModels = loadService().selectList(Wrappers.<PackageSkuAttrModel>lambdaQuery()
                .isNull(PackageSkuAttrModel::getParentId)
                .eq(PackageSkuAttrModel::getEnable, true)
                .and(c -> c.isNull(PackageSkuAttrModel::getExamId)
                        .or().eq(PackageSkuAttrModel::getExamId, "")
                        .or().eq(StrUtil.isNotBlank(examId), PackageSkuAttrModel::getExamId, examId))
                .orderByAsc(PackageSkuAttrModel::getSort));
        packageSkuAttrModels.parallelStream().forEach(r -> r.setChildren(loadService().selectList(Wrappers.<PackageSkuAttrModel>lambdaQuery()
                .eq(PackageSkuAttrModel::getParentId, r.getId())
                .eq(PackageSkuAttrModel::getEnable, true)
                .and(c -> c.isNull(PackageSkuAttrModel::getExamId)
                        .or().eq(PackageSkuAttrModel::getExamId, "")
                        .or().eq(StrUtil.isNotBlank(examId), PackageSkuAttrModel::getExamId, examId))
                .orderByAsc(PackageSkuAttrModel::getSort))));
        return packageSkuAttrModels.stream().filter(p -> CollUtil.isNotEmpty(p.getChildren())).toList();
    }
    @PostMapping("/auto-add")
    public PackageSkuAttrModel autoAddSkuAttrByCategoryId(@RequestParam String value,@RequestParam String pid,@RequestParam String examId) {
        Long aLong = this.loadService().selectCount(Wrappers.<PackageSkuAttrModel>lambdaQuery().eq(PackageSkuAttrModel::getParentId, pid).eq(PackageSkuAttrModel::getExamId,examId).eq(PackageSkuAttrModel::getName, value));
        if (aLong != 0) {
            throw new BizRuntimeException("已存在sku属性");
        }
        PackageSkuAttrModel skuAttrModel = this.loadService().getById(pid);
        var sku = new PackageSkuAttrModel();
        sku.setId("sku_" + skuAttrModel.getExamId() + "_" + IdUtil.getSnowflakeNextIdStr());
        sku.setExamId(skuAttrModel.getExamId());
        sku.setName(value);
        sku.setEnable(true);
        sku.setIndustryId(skuAttrModel.getIndustryId());
        sku.setExamId(examId);
        sku.setParentId(pid);
        return packageSkuAttrService.insert(sku);
    }
}
